package com.orhon.smartcampus.modules.base.fetcher;

import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.base.dao.SchoolTypeDao;
import com.orhon.smartcampus.modules.base.entity.SchooltypesDeleateable;
import com.orhon.smartcampus.modules.base.gqlModel.GQLPageParam;
import com.orhon.smartcampus.modules.base.gqlModel.GQLSchoolType;
import com.orhon.smartcampus.modules.base.mm.EnvArgsMapper;
import com.orhon.smartcampus.modules.base.mm.SchoolTypeMapper;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class SchoolTypesFetcher implements DataFetcher {

	@Autowired
	private SchoolTypeDao schoolTypeDao;

	@Override
	public Object get(DataFetchingEnvironment env) throws Exception {
		List<GQLSchoolType> GQLSchoolType = new ArrayList<>();

		GQLPageParam page = EnvArgsMapper.MAPPER.fromArgs(env);
		String hql = "from SchooltypesDeleateable where deleted_at = null ";
		List<SchooltypesDeleateable> list = schoolTypeDao.find(hql,null,page.getPage(),page.getLimit());
		for (SchooltypesDeleateable item : list) {
			GQLSchoolType gqlSchoolType = SchoolTypeMapper.MAPPER.fromEntity(item);
			GQLSchoolType.add(gqlSchoolType);
		}

		Long total = 0L;
		if (env.getSelectionSet().contains("total")){
			total = schoolTypeDao.getTotalCount(hql);
		}
		return Result.ok().put("data", GQLSchoolType).put("total", total);
	}


}
